﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SqlBlocks.Gui
{
    public partial class Main : Form
    {
        NotifyIconEx notifyIcon;
        SqlFileManager manager = null;

        public Main()
        {
            InitializeComponent();

            notifyIcon = new NotifyIconEx();
            notifyIcon.Visible = true;
            notifyIcon.Text = "SqlBlocks Helper";
            notifyIcon.DoubleClick += new EventHandler(notifyIcon_DoubleClick);
            notifyIcon.Icon = new Icon(GetType(), "database_lightning.ico");
            notifyIcon.ContextMenu = MainMenu;

            this.Icon = notifyIcon.Icon;

            this.Resize += new EventHandler(Main_Resize);
        }

        void Main_Resize(object sender, EventArgs e)
        {
            this.ShowInTaskbar = this.WindowState == FormWindowState.Minimized ? false : true;
        }

        void notifyIcon_DoubleClick(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
        }

        public Main(string directory)
            : this()
        {
            if (!string.IsNullOrEmpty(directory))
            {
                WatchDirectory(directory);
            }
            else
            {
                this.WindowState = FormWindowState.Normal;
            }
        }

        private void btnChooseMigrationFolder_Click(object sender, EventArgs e)
        {
            SqlFileToWatchChooser.SelectedPath = 
                Settings.Current.LastDirectory ?? 
                Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            var dialog = SqlFileToWatchChooser.ShowDialog(this);

            if (dialog == DialogResult.OK)
            {
                WatchDirectory(SqlFileToWatchChooser.SelectedPath);
            }
        }

        void WatchDirectory(string directory)
        {
            manager = new SqlFileManager(directory);
            manager.Found += new FileFound(manager_Found);

            Settings.Current.LastDirectory = directory;

            notifyIcon.ShowBalloon(
                "Watching Directory",
                "Looking for migrations in " + directory,
                NotifyIconEx.NotifyInfoFlags.Info,
                10);
        }

        delegate void PossibleInvoke();
        void manager_Found(SqlFileManager.Message message, EventArgs e)
        {
            PossibleInvoke method = delegate
            {
                notifyIcon.ShowBalloon(
                    "Processing " + message.File.Name,
                    message.Description,
                    NotifyIconEx.NotifyInfoFlags.Info,
                    10
                    );
            };

            if (this.InvokeRequired) this.Invoke(method);
            else method();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var di = new System.IO.DirectoryInfo(Utility.UserDataDirectory);
            MessageBox.Show(di.FullName);
        }

        private void MainMenuExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            Settings.Save();
        }
    }
}
